package com.ruicar.afs.cloud.apply.pre.loan.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOprRecord;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.loan.condition.OrderOprCondition;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOprRecordService;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 操作记录controller
 *
 * @author 耿继辉
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/opr")
public class ApplyOprRecordController {

    private final ApplyOprRecordService oprRecordService;

    /**
     * 多条件获取订单操作信息
     *
     * @param condition
     * @return
     */
    @ApiOperation("多条件获取订单操作信息")
    @PostMapping("/getOprRecord")
    public IResponse<List<ApplyOprRecord>> getOprRecord(@RequestBody QueryCondition<OrderOprCondition> condition) {
        if (StringUtil.isNotEmpty(condition.getCondition().getStartDate())) {
            condition.getCondition().setStartDate(condition.getCondition().getStartDate() + " 23:59:59");
        }
        if (StringUtil.isNotEmpty(condition.getCondition().getEndDate())) {
            condition.getCondition().setEndDate(condition.getCondition().getEndDate() + " 23:59:59");
        }
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        IPage<List<ApplyOprRecord>> oprRecord = oprRecordService.getOprRecord(condition.getCondition(), page);
        return IResponse.success(oprRecord);
    }


    /**
     * 搜索资产变更
     *
     * @param condition
     * @return
     */
    @ApiOperation("搜索资产变更")
    @PostMapping("/searchOprInfoForAssertChange")
    public IResponse<List<ApplyOprRecord>> searchOprInfoForAssertChange(@RequestBody QueryCondition<OrderOprCondition> condition) {
        if (StringUtil.isNotEmpty(condition.getCondition().getStartDate())) {
            condition.getCondition().setStartDate(condition.getCondition().getStartDate() + " 23:59:59");
        }
        if (StringUtil.isNotEmpty(condition.getCondition().getEndDate())) {
            condition.getCondition().setEndDate(condition.getCondition().getEndDate() + " 23:59:59");
        }
        JSONArray ids = SecurityUtils.getUser().getUserExtInfo().getJSONArray(ApplyConstants.DEPT_IDS);
        if (ids != null) {
            condition.getCondition().setCreateBy(SecurityUtils.getUsername());
            condition.getCondition().setDeptIds(ids);
        }
        JSONArray roleArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("roles");
        if (roleArray != null) {
            if (roleArray.toString().indexOf(ApplyConstants.ROLE_ADMIN) > -1) {
                return IResponse.success(null);
            } else {
                Page page = new Page(condition.getPageNumber(), condition.getPageSize());
                IPage<ApplyOprRecord> oprRecord = oprRecordService.searchOprInfoForAssertChange(condition.getCondition(), page);
                return IResponse.success(oprRecord);
            }
        } else {
            return IResponse.fail("此账号未配置权限,请检查账号！");
        }
    }

    /**
     * 搜索复议
     *
     * @param condition
     * @return
     */
    @ApiOperation("搜索复议")
    @PostMapping("/searchOprInfoForReconsider")
    public IResponse<List<ApplyOprRecord>> searchOprInfoForReconsider(@RequestBody QueryCondition<OrderOprCondition> condition) {
        if (StringUtil.isNotEmpty(condition.getCondition().getStartDate())) {
            condition.getCondition().setStartDate(condition.getCondition().getStartDate() + " 23:59:59");
        }
        if (StringUtil.isNotEmpty(condition.getCondition().getEndDate())) {
            condition.getCondition().setEndDate(condition.getCondition().getEndDate() + " 23:59:59");
        }
        JSONArray ids = SecurityUtils.getUser().getUserExtInfo().getJSONArray(ApplyConstants.DEPT_IDS);
        if (ids != null) {
            condition.getCondition().setCreateBy(SecurityUtils.getUsername());
            condition.getCondition().setDeptIds(ids);
        }
        JSONArray roleArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("roles");
        if (roleArray != null) {
            if (roleArray.toString().indexOf(ApplyConstants.ROLE_ADMIN) > -1) {
                return IResponse.success(null);
            } else {
                Page page = new Page(condition.getPageNumber(), condition.getPageSize());
                IPage<ApplyOprRecord> oprRecord = oprRecordService.searchOprInfoForReconsider(condition.getCondition(), page);
                return IResponse.success(oprRecord);
            }
        } else {
            return IResponse.fail("此账号未配置权限,请检查账号！");
        }
    }


    /**
     * 更新操作记录信息
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/updateOprRecord")
    @ApiOperation("更新操作记录信息")
    public IResponse updateOprRecord(@RequestBody OrderOprCondition condition) {
        oprRecordService.updateOprRecord(condition);
        return IResponse.success("更新成功");
    }

    /**
     * 更新del_flag
     *
     * @param condition
     * @return
     */
    @PostMapping("/updateDelFlag")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("更新操作记录信息")
    public IResponse updateDelFlag(@RequestBody OrderOprCondition condition) {
        oprRecordService.removeById(condition.getId());
        return IResponse.success("删除成功");
    }


}
